//
//  HomeViewController.m
//  GitTestDemo
//
//  Created by XFB on 2017/5/20.
//  Copyright © 2017年 ZC. All rights reserved.
//

#import "HomeViewController.h"

// Controller
#import "WeiBoViewController.h"
#import "WeChatViewController.h"
#import "QQViewController.h"

// SDK
#import "UMMobClick/MobClick.h"

// U-Share核心SDK
#import <UMSocialCore/UMSocialCore.h>
// U-Share分享面板SDK，未添加分享面板SDK可将此行去掉
#import <UShareUI/UShareUI.h>

@interface HomeViewController ()

@end

@implementation HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.pageName = @"HomeViewController";
    
    UIButton *weiboLogin = [[UIButton alloc] initWithFrame:CGRectMake(50, 100, 100, 40)];
    weiboLogin.backgroundColor = [UIColor orangeColor];
    [weiboLogin setTitle:@"微博登录" forState:UIControlStateNormal];
    [weiboLogin addTarget:self action:@selector(weiboClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:weiboLogin];
    
    UIButton *weChatLogin = [[UIButton alloc] initWithFrame:CGRectMake(50, 160, 100, 40)];
    weChatLogin.backgroundColor = [UIColor redColor];
    [weChatLogin setTitle:@"微信登录" forState:UIControlStateNormal];
    [weChatLogin addTarget:self action:@selector(weChatClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:weChatLogin];
    
    UIButton *qqLogin = [[UIButton alloc] initWithFrame:CGRectMake(50, 220, 100, 40)];
    qqLogin.backgroundColor = [UIColor purpleColor];
    [qqLogin setTitle:@"QQ登录" forState:UIControlStateNormal];
    [qqLogin addTarget:self action:@selector(qqClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:qqLogin];
    
    
    UIButton *weiboShare = [[UIButton alloc] initWithFrame:CGRectMake(200, 100, 100, 40)];
    weiboShare.backgroundColor = [UIColor orangeColor];
    [weiboShare setTitle:@"微博分享" forState:UIControlStateNormal];
    [weiboShare addTarget:self action:@selector(weiboShareClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:weiboShare];
    
    UIButton *weChatShare = [[UIButton alloc] initWithFrame:CGRectMake(200, 160, 100, 40)];
    weChatShare.backgroundColor = [UIColor redColor];
    [weChatShare setTitle:@"微信分享" forState:UIControlStateNormal];
    [weChatShare addTarget:self action:@selector(weChatShareClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:weChatShare];
    
    UIButton *qqShare = [[UIButton alloc] initWithFrame:CGRectMake(200, 220, 100, 40)];
    qqShare.backgroundColor = [UIColor purpleColor];
    [qqShare setTitle:@"QQ分享" forState:UIControlStateNormal];
    [qqShare addTarget:self action:@selector(qqShareClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:qqShare];
    
}

#pragma mark --- <微博、微信、QQ第三方 登录Login>
- (void)weiboClick
{
    NSLog(@"\n");
    [MobClick event:@"10000"];
    
    WeiBoViewController *vc = [[WeiBoViewController alloc] init];
    [self presentViewController:vc animated:YES completion:nil];
}

- (void)weChatClick
{
    NSLog(@"\n");
    [MobClick event:@"10001"];
    
    WeChatViewController *vc = [[WeChatViewController alloc] init];
    [self presentViewController:vc animated:YES completion:nil];
}

- (void)qqClick
{
    NSLog(@"\n");
    [MobClick event:@"10002"];
    
    QQViewController *vc = [[QQViewController alloc] init];
    [self presentViewController:vc animated:YES completion:nil];
}


#pragma mark --- <微博、微信、QQ第三方 分享Share>

- (void)weiboShareClick
{

}

- (void)weChatShareClick
{

}

- (void)qqShareClick
{

}


- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self shareWebPageToPlatformType:UMSocialPlatformType_Sina];
}

// 分享到第三方社交平台
- (void)shareWebPageToPlatformType:(UMSocialPlatformType)platformType
{
    //创建分享消息对象
    UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
    
    //创建网页内容对象
    NSString* thumbURL =  @"https://mobile.umeng.com/images/pic/home/social/img-1.png";
    UMShareWebpageObject *shareObject = [UMShareWebpageObject shareObjectWithTitle:@"欢迎使用【友盟+】社会化组件U-Share" descr:@"欢迎使用【友盟+】社会化组件U-Share，SDK包最小，集成成本最低，助力您的产品开发、运营与推广！" thumImage:thumbURL];
    //设置网页地址
    shareObject.webpageUrl = @"http://mobile.umeng.com/social";
    
    //分享消息对象设置分享内容对象
    messageObject.shareObject = shareObject;
    
    //调用分享接口
    [[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:self completion:^(id data, NSError *error) {
        if (error) {
            
            
            UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"提示" message:@"分享取消" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *action = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            }];
            [alertVC addAction:action];
            [self presentViewController:alertVC animated:YES completion:nil];

            UMSocialLogInfo(@"************Share fail with error %@*********",error);
        }else{
            if ([data isKindOfClass:[UMSocialShareResponse class]]) {
                UMSocialShareResponse *resp = data;
                //分享结果消息
                UMSocialLogInfo(@"response message is %@",resp.message);
                //第三方原始返回的数据
                UMSocialLogInfo(@"response originalResponse data is %@",resp.originalResponse);
                
            }else{
                UMSocialLogInfo(@"response data is %@",data);
                
                UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"提示" message:@"分享成功" preferredStyle:UIAlertControllerStyleAlert];
                UIAlertAction *action = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                }];
                UIAlertAction *actionCancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                }];
                
                [alertVC addAction:action];
                [alertVC addAction:actionCancel];
                [self presentViewController:alertVC animated:YES completion:nil];
                
            }
        }
//        [self alertWithError:error];
    }];
}

@end
